package com.kuang.controller;

import com.kuang.pojo.Book;
import com.kuang.pojo.Category;
import com.kuang.pojo.LendInfo;
import com.kuang.pojo.Reader;
import com.kuang.service.BookService;
import com.kuang.service.LendInfoSerivce;
import com.kuang.util.AjaxResult;
import com.kuang.util.Const;
import com.kuang.util.PageBean;
import com.kuang.util.StringUtil;
import org.activiti.engine.impl.util.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Author: StarSea99
 * @Date: 2020-08-31 17:34
 */
@Controller
@RequestMapping("/library")
public class libraryController {

    @Autowired
    private BookService bookService;
    @Autowired
    private LendInfoSerivce lendInfoSerivce;

    // 跳转展示页，并显示类别
    @RequestMapping("/index")
    public String index(HttpSession session){
        //获取类别
        List<Category> categoryList = bookService.listCategory();
        session.setAttribute(Const.CATEGORY,categoryList);
        return "index";
    }

    // 跳转读者借阅界面
    @RequestMapping("/frontIndex")
    public String frontIndex(){
        return "frontIndex";
    }

    //显示全部图书
    @RequestMapping("/listBook")
    @ResponseBody
    public String listBook(
            @RequestParam(value = "page",defaultValue = "1") Integer pageno,
            @RequestParam(value = "limit",defaultValue = "5") Integer pagesize,
            String bname, String author, String cid, HttpSession session){

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageno",pageno);
        paramMap.put("pagesize",pagesize);
        // 判断是否为空
        if (StringUtil.isNotEmpty("bname")){
            paramMap.put("bname",bname);
        }
        if (StringUtil.isNotEmpty("author")){
            paramMap.put("author",author);
        }
        if (StringUtil.isNotEmpty("cid")){
            paramMap.put("cid",cid);
        }
        // 分页查询
        PageBean<Book> pageBean = bookService.queryBookPage(paramMap);

        // 获取类别
        List<Category> listCategory = bookService.listCategory();
        session.setAttribute(Const.CATEGORY,listCategory);

        // list 转换为json
        JSONObject obj = new JSONObject();
        // Layui table 组件要求返回的格式
        obj.put("code", 0);
        obj.put("msg", "");
        obj.put("count",pageBean.getTotalsize());
        obj.put("data", pageBean.getDatas());
        return obj.toString();
    }

    // 跳转添加页面
    @RequestMapping("/addBook")
    public String addBook(){
        return "book/addBook";
    }

    // 添加图书
    @RequestMapping("/submitAddBook")
    @ResponseBody
    public AjaxResult submitAddBook(Book book){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            bookService.addBook(book);
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加失败");
        }
        return ajaxResult;
    }

    // 跳转修改页面
    @RequestMapping("/editBook")
    public String editBook(String book_id, Model model){
        Book book = bookService.selectById(Integer.parseInt(book_id));
        model.addAttribute("bookinfo",book);
        model.addAttribute("code",1);
        return "book/addBook";
    }

    // 修改图书
    @RequestMapping("/updateBook")
    @ResponseBody
    public AjaxResult updateBook(Book book){
        AjaxResult ajaxResult = new AjaxResult();

        try {
            bookService.updateBook(book);
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改失败");
        }
        return ajaxResult;
    }

    // 图书查看详情页
    @RequestMapping("/findBook")
    public String findBook(String book_id,Model model){
        Book book = bookService.selectById(Integer.parseInt(book_id));
        model.addAttribute("bookinfo",book);
        return "book/addBook";
    }

    //删除图书
    @RequestMapping("/delBook")
    @ResponseBody
    public AjaxResult delBook(String book_id) {
        AjaxResult ajaxResult = new AjaxResult();
        try{
            bookService.delBook(Integer.parseInt(book_id));
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除失败");
        }
        return ajaxResult;
    }

    // 读者借阅图书
    @RequestMapping("/lendBook")
    @ResponseBody
    public AjaxResult lendBook(Integer book_id,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();

        Reader reader = (Reader) session.getAttribute(Const.READER);

        // 判断库存图书够不够
        Book book = bookService.selectById(book_id);
        if (book.getStock() == 0){
            ajaxResult.setStatus("2");
            return ajaxResult;
        }

        // 判断该读者是否已经借阅过该图书
        LendInfo lendInfo = new LendInfo();
        lendInfo.setBook_id(book_id);
        lendInfo.setReader_id(reader.getReader_id());
        if (lendInfoSerivce.isLended(lendInfo)){
            ajaxResult.setStatus("0");
            return ajaxResult;
        }

        //判断是否达到借书上限
        Integer cardState = lendInfoSerivce.cardState(reader.getReader_id());
        if (cardState.equals(reader.getCard_state())){
            ajaxResult.setStatus("3");
            return ajaxResult;
        }

        // 借书成功
        lendInfoSerivce.lendBook(lendInfo);
        ajaxResult.setStatus("1");
        return ajaxResult;
    }


}
